import { PoolManager } from "../Managers/PoolManager";

const { ccclass, property } = cc._decorator;
const oriPos: cc.Vec3 = cc.v3(0, 100, 0);

@ccclass
export default class Tip extends cc.Component {
    mask: cc.Node = null!;
    tipLb: cc.Label = null!;

    onLoad() {
        this.mask = this.node.getChildByName(`Mask`);
        this.tipLb = this.node.getChildByName(`TipLb`).getComponent(cc.Label);
    }

    Show(content: string, delay: number = 0.75, tweenType: number = 1) {
        this.node.opacity = 255;

        this.tipLb.string = `${content}`;
        //  this.tipLb.updateRenderData(true);
        // this.scheduleOnce(() => {
        //     this.mask.setContentSize(this.tipLb.node.getContentSize().width + 200, this.tipLb.node.getContentSize().height + 20);
        // });

        switch (tweenType) {
            case 1:
                this.ShowTween1(delay);
                break;
            case 2:
                this.ShowTween2(delay);
                break;
            default:
                this.ShowTween1(delay);
                break;
        }
    }

    ShowTween1(delay: number = 0.75) {
        this.node.setScale(0, 0, 0);
        this.node.setPosition(oriPos);
        cc.Tween.stopAllByTarget(this.node);
        cc.tween(this.node)
            .to(0.5, { scale: 1 }, { easing: `elasticOut` })
            .delay(delay)
            .to(0.25, { position: cc.v3(0, 250), scale: 0 })
            .call(() => {
                PoolManager.PutNode(this.node);
            })
            .start();
    }

    ShowTween2(delay: number = 0.75) {
        this.node.setScale(1);
        this.node.setPosition(oriPos);
        cc.Tween.stopAllByTarget(this.node);
        cc.tween(this.node).delay(delay).to(0.25, { position: cc.v3(0, 250), opacity: 0 }).call(() => { PoolManager.PutNode(this.node); }).start();
    }
}
